//
//  NMPOVpnResultCell.swift
//  NMPO
//
//  Created by PowerfullClean on 2023/10/31.
//

import UIKit

class NMPOVpnResultCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(nmpo_bgView)
        contentView.addSubview(nmpo_iconView)
        contentView.addSubview(nmpo_titleLabel)
        contentView.addSubview(nmpo_bottomView)
        contentView.addSubview(nmpo_timeLabel)
        contentView.addSubview(nmpo_successLabel)
        contentView.addSubview(nmpo_iPValueLabel)
        nmpo_bgView.snp.makeConstraints { make in
            make.top.left.width.equalToSuperview()
            make.height.equalTo(243)
        }
        nmpo_iconView.snp.makeConstraints { make in
            make.size.equalTo(148)
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(6)
        }
        nmpo_titleLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-14)
            make.top.equalToSuperview().offset(25)
        }
        nmpo_bottomView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 332, height: 68))
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-4)
        }
        nmpo_timeLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-14)
            make.top.equalTo(nmpo_iPValueLabel.snp.bottom).offset(4)
        }
        nmpo_successLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-14)
            make.top.equalTo(nmpo_timeLabel.snp.bottom).offset(4)
        }
        nmpo_iPValueLabel.snp.makeConstraints { make in
            make.right.equalTo(nmpo_titleLabel.snp.right)
            make.top.equalTo(nmpo_titleLabel.snp.bottom).offset(10)
        }
        nmpo_startConnectTimer()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc func nmpo_connectClick() {
        NMPOManager.nmpo_manager.nmpo_smart = true
        NMPOManager.nmpo_manager.nmpo_best = false
        if NMPOManager.nmpo_manager.nmpo_vpnCurStateType == .nmpo_connected {
            NMPOManager.nmpo_manager.nmpo_vpnStateClickType = .nmpo_disConnect
        }else {
            NMPOManager.nmpo_manager.nmpo_vpnStateClickType = .nmpo_connect
        }
        
        NMPOManager.nmpo_manager.nmpo_connectSmartAction()
        nmpo_viewGetVController?.navigationController?.popToRootViewController(animated: true)
    }
    deinit {
        nmpo_stopConnectTimer()
    }
    var nmpo_serverModel: NMPOSerModel? {
        didSet {
            guard let nmpo_serverModel = nmpo_serverModel else { return  }
            nmpo_iPValueLabel.text = nmpo_serverModel.nmpo_address
            nmpo_successLabel.text = "Successfully connected to VPN\n\nstart to travel the world"
            if NMPOManager.nmpo_manager.nmpo_best {
                nmpo_successLabel.text = "Successfully connected to\nBest servers"
            }
            if NMPOManager.nmpo_manager.nmpo_smart {
                nmpo_successLabel.text = "Successfully connected to\nStable servers"
            }

            if nmpo_serverModel.nmpo_address == "38.114.114.19" {
                nmpo_titleLabel.text = "Los Angeles"
            }
            if nmpo_serverModel.nmpo_address == "38.68.134.105" {
                nmpo_titleLabel.text = "Dallas"
            }
            if nmpo_serverModel.nmpo_address == "38.107.226.61" {
                nmpo_titleLabel.text = "Ashburn 3"
            }
            if nmpo_serverModel.nmpo_address == "216.39.248.160" {
                nmpo_titleLabel.text = "Chicago"
            }
            if nmpo_serverModel.nmpo_address == "139.64.165.239" {
                nmpo_titleLabel.text = "Denver"
            }
            if nmpo_serverModel.nmpo_address == "38.114.114.155" {
                nmpo_titleLabel.text = "Los Angeles"
            }
            if nmpo_serverModel.nmpo_address == "216.39.248.169" {
                nmpo_titleLabel.text = "Chicago"
            }
            if nmpo_serverModel.nmpo_address == "38.75.137.125" {
                nmpo_titleLabel.text = "Los Angeles"
            }
            if nmpo_serverModel.nmpo_address == "162.251.63.93" {
                nmpo_titleLabel.text = "Los Angeles"
            }
            if nmpo_serverModel.nmpo_address == "216.173.115.84" {
                nmpo_titleLabel.text = "Ashburn"
            }
            if nmpo_serverModel.nmpo_address == "38.64.138.184" {
                nmpo_titleLabel.text = "Chicago"
            }
            if nmpo_serverModel.nmpo_address == "38.107.226.200" {
                nmpo_titleLabel.text = "Ashburn"
            }
            if nmpo_serverModel.nmpo_address == "38.86.135.202" {
                nmpo_titleLabel.text = "Ashburn"
            }
            if nmpo_serverModel.nmpo_address == "38.107.226.152" {
                nmpo_titleLabel.text = "Ashburn 1"
            }
            if nmpo_serverModel.nmpo_address == "38.86.135.250" {
                nmpo_titleLabel.text = "Ashburn"
            }
            if nmpo_serverModel.nmpo_address == "38.86.135.74" {
                nmpo_titleLabel.text = "Ashburn"
            }
            if nmpo_serverModel.nmpo_address == "38.107.226.127" {
                nmpo_titleLabel.text = "Ashburn"
            }
            if nmpo_serverModel.nmpo_address == "38.121.43.235" {
                nmpo_titleLabel.text = "Atlanta"
            }
            if nmpo_serverModel.nmpo_address == "38.121.43.235" {
                nmpo_titleLabel.text = "Atlanta"
            }
            if nmpo_serverModel.nmpo_address == "216.39.248.251" {
                nmpo_titleLabel.text = "Chicago"
            }
            if nmpo_serverModel.nmpo_address == "38.64.138.244" {
                nmpo_titleLabel.text = "Chicago"
            }
            if nmpo_serverModel.nmpo_address == "216.39.248.225" {
                nmpo_titleLabel.text = "Chicago"
            }
            if nmpo_serverModel.nmpo_address == "38.64.138.5" {
                nmpo_titleLabel.text = "Chicago"
            }
            if nmpo_serverModel.nmpo_address == "38.121.43.151" {
                nmpo_titleLabel.text = "Atlanta 1"
            }
            if nmpo_serverModel.nmpo_address == "38.107.226.223" {
                nmpo_titleLabel.text = "Ashburn"
            }
            if nmpo_serverModel.nmpo_address == "38.86.135.106" {
                nmpo_titleLabel.text = "Ashburn"
            }
            if nmpo_serverModel.nmpo_address == "38.86.135.236" {
                nmpo_titleLabel.text = "Ashburn"
            }
            if nmpo_serverModel.nmpo_address == "142.202.49.114" {
                nmpo_titleLabel.text = "Ashburn 2"
            }
            if nmpo_serverModel.nmpo_address == "38.121.43.78" {
                nmpo_titleLabel.text = "Atlanta"
            }
            if nmpo_serverModel.nmpo_address == "38.64.138.195" {
                nmpo_titleLabel.text = "Chicago"
            }
            if nmpo_serverModel.nmpo_address == "38.68.135.115" {
                nmpo_titleLabel.text = "Dallas"
            }
            if nmpo_serverModel.nmpo_address == "38.121.43.44" {
                nmpo_titleLabel.text = "Atlanta 2"
            }
            if nmpo_serverModel.nmpo_address ==  "38.91.106.154" {
                nmpo_titleLabel.text = "Miami"
            }
            if nmpo_serverModel.nmpo_address ==  "38.91.106.154" {
                nmpo_titleLabel.text = "Miami"
            }
            if nmpo_serverModel.nmpo_address ==  "139.64.165.86" {
                nmpo_titleLabel.text = "Denver"
            }
            
            if nmpo_titleLabel.text?.count ?? 0 > 0 {
                nmpo_serverModel.nmpo_country = "USA"
                nmpo_titleLabel.text = (nmpo_serverModel.nmpo_country ?? "") + "-" + (nmpo_titleLabel.text ?? "")
            }else {
                nmpo_titleLabel.text = nmpo_serverModel.nmpo_country
            }
        }
    }
    lazy var nmpo_iPValueLabel: UILabel = {
        let nmpo_valueLabel = UILabel()
        nmpo_valueLabel.font = UIFont.systemFont(ofSize: 16)
        nmpo_valueLabel.textColor = NMPOCommon.nmpo_getHXColorValue(rgb: 0xFFFFFF)
        nmpo_valueLabel.text = ""
        return nmpo_valueLabel
    }()
    lazy var nmpo_iconView: UIImageView = {
        let nmpo_iconView = UIImageView(image: UIImage(named: "nmpo_result_connectIcon"))
        return nmpo_iconView
    }()
    lazy var nmpo_titleLabel: UILabel = {
        let nmpo_titleLabel = UILabel()
        nmpo_titleLabel.font = UIFont.systemFont(ofSize: 10)
        nmpo_titleLabel.textColor = NMPOCommon.nmpo_getHXColorValue(rgb: 0xFFFFFF)
       
        return nmpo_titleLabel
    }()
    lazy var nmpo_bgView: UIImageView = {
        let nmpo_bgView = UIImageView(image: UIImage(named: "nmpo_result_connectbg"))
        return nmpo_bgView
    }()
    lazy var nmpo_bottomView: UIImageView = {
        let nmpo_bottomView = UIImageView(image: UIImage(named: "nmpo_result_connectBottom"))
        nmpo_bottomView.nmpo_addTap(target: self, action: #selector(nmpo_connectClick))
        return nmpo_bottomView
    }()
    lazy var nmpo_timeLabel: UILabel = {
        let nmpo_timeLabel = UILabel()
        nmpo_timeLabel.font = UIFont.systemFont(ofSize: 24)
        nmpo_timeLabel.textColor = NMPOCommon.nmpo_getHXColorValue(rgb: 0xFFFFFF)
        nmpo_timeLabel.text = "00:01:00"
        return nmpo_timeLabel
    }()
    lazy var nmpo_successLabel: UILabel = {
        let nmpo_successLabel = UILabel()
        nmpo_successLabel.font = UIFont.systemFont(ofSize: 12)
        nmpo_successLabel.textColor = NMPOCommon.nmpo_getHXColorValue(rgb: 0xFFFFFF)
        nmpo_successLabel.text = "Successfully connected to VPN\n\nstart to travel the world"
        nmpo_successLabel.numberOfLines = 0
        nmpo_successLabel.textAlignment = .right
        return nmpo_successLabel
    }()
    var nmpo_connectName: String = "nmpo_connectName"
    var nmpo_connectCount:NSInteger = 0
}

extension NMPOVpnResultCell {
    fileprivate func nmpo_startConnectTimer() {
        nmpo_connectName = NMPOTimerVender.nmpo_timeTask(self, selector: #selector(nmpo_connectSum), start: 0, interval: 1, repeats: true, async: false, name: nmpo_connectName)
    }
    
    func nmpo_stopConnectTimer() {
        NMPOTimerVender.nmpo_cancelTask(nmpo_connectName)
        nmpo_connectName = "nmpo_connectName"
        nmpo_connectCount = 0
    }
    
    @objc fileprivate func nmpo_connectSum() {
        nmpo_connectCount = nmpo_connectCount + 1
        nmpo_timeLabel.text = nmpo_connectCount.nmpo_formatTime()
    }
    
}

